/*
 * TestAnimator.java
 *
 * Created on November 27, 2007, 7:27 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package simfinal;

import com.jme.animation.SpatialTransformer;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.image.Texture;
import com.jme.light.PointLight;
import com.jme.light.SimpleLightNode;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.scene.shape.Cone;
import com.jme.scene.state.LightState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.util.geom.BufferUtils;
import com.jmex.model.animation.KeyframeController;
import java.net.URL;
import java.nio.FloatBuffer;
//import jmetest.TutorialGuide.HelloStates;

import com.jmex.physics.util.SimplePhysicsGame;
//import javax.crypto.NullCipher;

/**
 *
 * @author Eric Drucker
 */
public class FinalAnimator extends SimplePhysicsGame
{
	private Box b = new Box("Mybox", new Vector3f(0,0,0), new Vector3f(1,1,1));
	private int eric = 0;
        
        Particle [] planets = null;
        Nbody e0 =  null;
        Vector p0 = null;
        Vector p1 = null;
        Vector p2 = null;
        Vector rp1 = null;
        
        Sphere planet1 = null;
        Sphere planet3 = null;
        Cone rocket = null;
        
        Rocket r1 = null;
	
	/** Creates a new instance of TestAnimator */
	public FinalAnimator()
	{
	}
	
	
	
	public static void main(String[] args)
	{
		//FinalPopulation p = new FinalPopulation();
            System.out.println("STARTING TEST ANIMATION");
		
		FinalAnimator app = new FinalAnimator(); // Create Object
		// Signal to show properties dialog
		app.setDialogBehaviour(SimpleGame.ALWAYS_SHOW_PROPS_DIALOG);
		app.start(); // Start the program
                
                
	}

	protected void simpleInitGame()
	{ 
		
		System.out.println("Hello3");
		planet1=new Sphere("planet1",10,10,0.1f);
		Sphere planet2=new Sphere("planet2",10,10,1);
		planet3=new Sphere("planet3",10,10,0.2f);
		Sphere planet4=new Sphere("planet4",20,20,50);
		Sphere planet5=new Sphere("planet5",20,20,50);
		System.out.println(planet1.getCenter());
                
                
		planet2.setSolidColor(ColorRGBA.blue);
		planet3.setSolidColor(ColorRGBA.red);
		planet4.setSolidColor(ColorRGBA.orange);
		planet5.setSolidColor(ColorRGBA.yellow);
		
		rocket = new Cone("rp1",10,10,.1f,1);
                rocket.setLocalTranslation(100, 100, -100);
		
                
		planet1.setLocalTranslation(new Vector3f(100, 10, 0));
		planet2.setLocalTranslation(new Vector3f(0, 0, 0));
		planet3.setLocalTranslation(new Vector3f(30, 30, 0));
		planet4.setLocalTranslation(new Vector3f(40, 40, 0));
		planet5.setLocalTranslation(new Vector3f(50, 50, 0));
		
		// Do bounds for the sphere, but we'll use a BoundingBox this time
		planet1.setModelBound(new BoundingBox());
		planet2.setModelBound(new BoundingBox());
		planet3.setModelBound(new BoundingBox());
		planet4.setModelBound(new BoundingBox());
		planet5.setModelBound(new BoundingBox(new Vector3f(1000,1000,1000),1000,1000,1000));
                
                rocket.setModelBound(new BoundingBox());
                rocket.updateModelBound();
                
		planet1.updateModelBound();
		planet2.updateModelBound();
		planet3.updateModelBound();
		planet4.updateModelBound();
		planet5.updateModelBound();
		// Give the sphere random colors
		
                
                		
		Sphere s=new Sphere("My sphere",10,10,.1f);
		// Do bounds for the sphere, but we'll use a BoundingBox this time
		s.setModelBound(new BoundingBox(new Vector3f(1000,1000,1000),1000,1000,1000));
		s.updateModelBound();
		// Give the sphere random colors
		//s.setRandomColors();
		
		planet1.setSolidColor(ColorRGBA.green);
                rocket.setSolidColor(ColorRGBA.cyan);
		
		//rootNode.attachChild(planet1); // Put it in the scene graph
		rootNode.attachChild(planet2); // Put it in the scene graph
		//rootNode.attachChild(planet3); // Put it in the scene graph
	//	rootNode.attachChild(planet4); // Put it in the scene graph
	//	rootNode.attachChild(planet5); // Put it in the scene graph
                
                rootNode.attachChild(rocket);
		
                rootNode.setLightCombineMode(LightState.OFF);
                rootNode.attachChild(s); // Put it in the scene graph
		
		
		s.addController(new PlanetRotator(s));
                
                planets = new Particle[3];
                
                planets[0] = new Particle();
                planets[0].set(1.0, new Vector(0.0,0.0,0.0), new Vector(0.0,0.0,0.0));
                planets[1] = new Particle();
                planets[1].set(0.0, new Vector(-0.1,0,0.0), new Vector(0,10.0,0));

                
                double r2 = 825/587.4;
                double theta = 0.0;
                double v = Math.sqrt(1.0/r2);
                planets[2] = new Particle();
                planets[2].set(0.0, new Vector(r2*Math.cos(theta),r2*Math.sin(theta),0.0),
                      new Vector(-v*Math.sin(theta),v*Math.cos(theta),0.0));
                 
                /*
                r1 = new Rocket("0011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "00111001001110010011100100111001001110010011100100111001001110010011100100111001" +
                        "",new Vector (1,0,0));
                 */
                
                
                r1 = new Rocket("1111111111111111111111111111111111111111111111111111111111111111111111" +
                     //   "11111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                     //   "11111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                     //   "11111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                     //   "11111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                     //   "11111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        //"111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "0010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        //"111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
				    "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        //"111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101" +
                        //"111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                        "010101010101010101010101010101010101010101010101010101010101010101010101010101010101"
                        ,new Vector (0,0,0));
                
                
                /*
                 r1 = new Rocket("11111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" +
                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
                         ,new Vector (0,0,0));
                 */
                
                
                //planets[3] = r1;
                //planets[3] = new Particle();
                //planets[3].set(0.0, new Vector(0.1,0,0.0), new Vector(0,-10.0,0));
                
                
                e0 = new Nbody(planets, 0.0);
                
                p0 = new Vector(e0.system[0].x0);
                p1 = new Vector(e0.system[1].x0);
                p2 = new Vector(e0.system[2].x0);
                //rp1 = new Vector(e0.system[2].x0);
                
                //System.out.println("Stuck?");
                
                
                
    }
	
	protected void simpleRender()
	{
		
	}
	
	class PlanetRotator extends Controller
	{
		float degree;
		TriMesh shape;
		
		PlanetRotator(TriMesh shape)
		{
			this.shape = shape;
			degree = 0;
		}

		public void update(float time)
		{
			//Quaternion q1 = new Quaternion(); //shape.getLocalRotation().w+1, shape.getLocalRotation().x+1, shape.getLocalRotation().y+1, shape.getLocalRotation().z+1
		//	q1.fromAngleAxis(shape.getLocalRotation().t, new Vector3f(1f, 0, 0));
			Quaternion q2 = new Quaternion();
                        
                        
			//System.out.println(shape.getLocalRotation());
                        
			//degree += 1f;
			//q1.fromAngleAxis(FastMath.DEG_TO_RAD*degree, new Vector3f(0,1,0));
                        
                        r1.Fire();
                        
                        //System.out.println("Before advance");
                        
                        //e0.advance(0.05);                 
                        
                        //System.out.println("After advance");

                        //p0 = new Vector(e0.system[0].x0);
                        //p1 = new Vector(e0.system[1].x0);
                        //p2 = new Vector(e0.system[2].x0);
                        //rp1 = new Vector(e0.system[2].x0);
                        
                                            
                        //System.out.println(v1.data[0] + " " + v1.data[1] + " " + v1.data[2]);
                        
                        //planet1.setLocalTranslation(p1.toVector3f().mult(10));
                        
                        //planet3.setLocalTranslation(p2.toVector3f().mult(10));
                        //planet1.setLocalTranslation(new Vector3f(0,1,0));
                        
                        rocket.setLocalTranslation(r1.x0dot.toVector3f().mult(.1f));
                        
                        q2.fromAngleAxis(r1.getRotation(), new Vector3f(0,1,0));
                        
                        rocket.setLocalRotation(q2);
				    
				    System.out.println(rocket.getLocalRotation());
				    System.out.println("Translation: " + rocket.getLocalTranslation());
			
			//shape.setLocalRotation(q1);
                        
                        
		}
	}
}

